#region MIT
/*
Blue.ResourceManager
Part of Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.IO;
using System.Collections.Generic;
using ICSharpCode.SharpZipLib.Zip;
using System.Diagnostics;

namespace Blue.ResourceManager
{	
    // The unit test for this don't work so this class is left out for now
    /* 
	public class ZipResourceManager : AbstractResourceManager
	{
		#region Variables
        String _resourcePath;
		ZipFile _zipfile;
        #endregion

        #region Properties
        public override bool IsReadOnly { get { return true; } }
        #endregion
		
		public ZipResourceManager( String resourcePath )
		{
			if ( String.IsNullOrEmpty( resourcePath ) )
            {
                throw new ArgumentNullException( "zipResourePath" );
            }

            resourcePath = resourcePath.Replace( '/', Path.DirectorySeparatorChar );
            resourcePath = resourcePath.Replace( '\\', Path.DirectorySeparatorChar );
			
			if ( !File.Exists( Path.GetFullPath( resourcePath ) ) )
			{
				throw new FileNotFoundException( "Could not find ZipResourcePath " + resourcePath );
			}
			
			_resourcePath = resourcePath;
			_zipfile = new ZipFile( _resourcePath );
		}
		
		#region Methods
		protected override void _renameResource( String oldName, String newName )
		{
			throw new NotSupportedException();
		}

        protected override Stream _openStream( String resourceName, ResourceOpenMode openMode )
		{
			resourceName = NormalizeName( resourceName );
			ZipEntry zipentry = null;
			
			if ( openMode == ResourceOpenMode.Read )
			{
				zipentry = _zipfile.GetEntry( resourceName );
				if ( zipentry == null )
                {
                    throw new ResourceManagerException( 
                        "The resource " + resourceName + " was not found.", 
                        ResourceManagerException.Reason.RESOURCE_NOT_FOUND 
                        );
                }
			
				return _zipfile.GetInputStream( zipentry );				
			}
			else
			{
				throw new ResourceManagerException(
                        "Write streams are not supported in read-only mode",
                        ResourceManagerException.Reason.NOT_SUPPORTED_FOR_READONLY
                    );
			}
		}		

        public override IEnumerable<String> FindResources( String searchDir, String ext )
		{
			Debug.Assert( File.Exists( Path.GetFullPath( _resourcePath ) ) );
			
			searchDir = this.NormalizeName( searchDir ) + Path.DirectorySeparatorChar;
			
			if ( _zipfile.GetEntry( searchDir ).IsDirectory )
			{
				ext = ( ( ext[0] == '.' ? "" : "." ) + ext );
				
				foreach ( ZipEntry e in _zipfile )
				{
					if ( e.Name.StartsWith( searchDir ) && e.Name.EndsWith( ext ) )
					{
						yield return this.NormalizeName( e.Name );						
					}
				}
			}			
			else
			{
				throw new ResourceManagerException( 
                        "The resource " + searchDir + " was not found.", 
                        ResourceManagerException.Reason.RESOURCE_NOT_FOUND 
                        );	
			}
		}

        public override IEnumerable<String> FindDirectories( String searchDir )
		{
			Debug.Assert( File.Exists( Path.GetFullPath( _resourcePath ) ) );
			
			searchDir = this.NormalizeName( searchDir ) + Path.DirectorySeparatorChar;
			
			if ( _zipfile.GetEntry( searchDir ).IsDirectory )
			{
				foreach (ZipEntry e in _zipfile )
				{
					if (e.Name.StartsWith( searchDir ) &&
					    e.Name.EndsWith( Path.DirectorySeparatorChar.ToString() ) &&
					    String.Compare( searchDir, e.Name ) != 0 )
					{
						yield return this.NormalizeName( e.Name );
					}
				}
			}
			else
			{
				throw new ResourceManagerException( 
                        "The resource " + searchDir + " was not found.", 
                        ResourceManagerException.Reason.RESOURCE_NOT_FOUND 
                        );	
			}
		}

        public override bool ResourceExists( String name )
		{
			if ( String.IsNullOrEmpty( name ) )
            {
                return false;
            }
			
			name = NormalizeName( name );
			
			if ( _zipfile.GetEntry( name ) != null )
			{
				if ( _zipfile.GetEntry( name ).IsFile )
				{
					return true;
				}
			}
			
			return false;
		}

        public override bool ResourceDirectoryExits( String name )
		{
			if ( String.IsNullOrEmpty( name ) )
            {
                return false;
            }
		
			name = NormalizeName( name ) + Path.DirectorySeparatorChar;
			
			if ( _zipfile.GetEntry( name ) != null )
			{
				if ( _zipfile.GetEntry( name ).IsDirectory )
				{
					return true;
				}
			}
			
			return false;
		}

        protected override void _createResourceDirectory( String name )
		{
			throw new NotSupportedException();
		}

        protected override void _renameResourceDirectory( String oldName, String newName )
		{
			throw new NotSupportedException();
		}

        protected override void _deleteResource( String name )
		{
			throw new NotSupportedException();
		}

        protected override void _deleteResourceDirectory( String name )
		{
			throw new NotSupportedException();
		}

        public override bool IsResourceDirectoryEmpty( String name )
		{
			name = NormalizeName( name ) + Path.DirectorySeparatorChar;
			
			foreach (ZipEntry e in _zipfile )
			{
				if (e.Name.StartsWith( name ) &&
					String.Compare( name, e.Name ) != 0 )
				{
					return false;
				}
			}
			
			return true;
		}
		#endregion
	}
	
	namespace Test
	{
		using NUnit.Framework;
		
		[TestFixture]
		public class ZipResourceManagerTest
		{
			string _path;
			DirectoryInfo _di;
			string _oneFileString;
			
			[SetUp]
			public void Init()
			{
				_path = @"testfiles.zip";
				_oneFileString = "a string value for testing reasons";
				string dirfortest = "dirfortest";
				string testdirectory = dirfortest + Path.DirectorySeparatorChar + "testdirectory";
				string subdir = testdirectory + Path.DirectorySeparatorChar + "subdir";
				string anothersubdir = testdirectory + Path.DirectorySeparatorChar + "anothersubdir";
				string subsubdir = anothersubdir + Path.DirectorySeparatorChar + "subsubdir";
				string subsubdir2 = anothersubdir + Path.DirectorySeparatorChar + "subsubdir2";
				
				if ( Directory.Exists( dirfortest ) )
				{
					Directory.Delete( dirfortest, true );
				}
				
				_di = Directory.CreateDirectory( dirfortest );
				
				if ( Directory.Exists( testdirectory ) )
				{
					Directory.Delete( testdirectory, true );
				}
				
				Directory.CreateDirectory( testdirectory );
				
				if ( Directory.Exists( subdir ) )
				{
					Directory.Delete( subdir, true );
				}
				
				Directory.CreateDirectory( subdir );
				
				if ( Directory.Exists( anothersubdir ) )
				{
					Directory.Delete( anothersubdir, true );
				}
				
				Directory.CreateDirectory( anothersubdir );
				
				if ( Directory.Exists( subsubdir ) )
				{
					Directory.Delete( subsubdir, true );
				}
				
				Directory.CreateDirectory( subsubdir );
				
				if ( Directory.Exists( subsubdir2 ) )
				{
					Directory.Delete( subsubdir2, true );
				}
				
				Directory.CreateDirectory( subsubdir2 );
				
				File.Create( subdir + Path.DirectorySeparatorChar + "subfile.test" ).Close();
				FileStream fs = File.Create( dirfortest + Path.DirectorySeparatorChar + "onefile.test" );
				
				using( StreamWriter writer = new StreamWriter( fs ) )
				{
					writer.Write( _oneFileString );
				}			
				
				File.Create( dirfortest + Path.DirectorySeparatorChar + "anotherfile.test" ).Close();
				
				FastZip zip = new FastZip();
				zip.CreateEmptyDirectories = true;
				zip.CreateZip( _path, dirfortest, true, null );
			}
			           
			[TearDown]
			public void Dispose()
			{
				if ( !ResourceManager.IsDisposed )
				{
					ResourceManager.Dispose();
				}
				
				_di.Delete( true );
				
				if ( File.Exists( _path ) )
				{
					File.Delete( _path );
				}
			}

			[Test]
			public void TestStream()
			{
				ZipResourceManager zrm = new ZipResourceManager( _path );
				ResourceManager.Initialize( zrm );
				Stream teststream = ResourceManager.OpenStream( "onefile.test", ResourceOpenMode.Read );
				string resultstring;

				using( StreamReader reader = new StreamReader( teststream ) )
				{
					resultstring = reader.ReadToEnd();
				}
				
				Assert.That( resultstring, Is.EqualTo( _oneFileString ) );
			}
			
			[Test]
			public void TestFindResources()
			{
				ZipResourceManager zrm = new ZipResourceManager( _path );
				ResourceManager.Initialize( zrm );
								
				List<String> resultlist = new List<String>();
								
				foreach( String str in ResourceManager.FindResources( "testdirectory", ".test" ) ) 
				{
					resultlist.Add( str );
				}
				
				List<String> shouldbe = new List<String>();
				shouldbe.Add( "testdirectory/subdir/subfile.test" );
				
				Assert.That( resultlist, Is.EquivalentTo( shouldbe ) );			
			}
			
			[Test]
			public void TestFindDirectories()
			{
				ZipResourceManager zrm = new ZipResourceManager( _path );
				ResourceManager.Initialize( zrm );
				
				List<String> resultlist = new List<String>();
								
				foreach( String str in ResourceManager.FindDirectories( "testdirectory/anothersubdir" ) )
				{
					resultlist.Add( str );
				}
				
				List<String> shouldbe = new List<String>();
				shouldbe.Add( "testdirectory/anothersubdir/subsubdir" );
				shouldbe.Add( "testdirectory/anothersubdir/subsubdir2" );
				
				Assert.That( resultlist, Is.EquivalentTo( shouldbe ) );
			}
			
			[Test]
			public void TestResourceExists()
			{
				ZipResourceManager zrm = new ZipResourceManager( _path );
				ResourceManager.Initialize( zrm );
				
				Assert.That( ResourceManager.ResourceExists( "testdirectory/subdir/subfile.test" ), Is.True );
				Assert.That( ResourceManager.ResourceExists( "onefile.test" ), Is.True );
			}
			
			[Test]
			public void TestDirectoryExists()
			{
				ZipResourceManager zrm = new ZipResourceManager( _path );
				ResourceManager.Initialize( zrm );
				
				Assert.That( ResourceManager.ResourceDirectoryExists( "testdirectory/anothersubdir/subsubdir2" ), Is.True );
				Assert.That( ResourceManager.ResourceDirectoryExists( "onefile.test" ), Is.False );
			}
			
			[Test]
			public void TestIsResourceDirectoryEmpty()
			{
				ZipResourceManager zrm = new ZipResourceManager( _path );
				ResourceManager.Initialize( zrm );
				
				Assert.That( ResourceManager.IsResourceDirectoryEmpty( "testdirectory/anothersubdir/subsubdir2/" ), Is.True );
				Assert.That( ResourceManager.IsResourceDirectoryEmpty( "testdirectory/subdir" ), Is.False );
			}
		}		
	}
    */
}














